package oakclub.utility;

import org.apache.commons.codec.binary.Base64;

import java.math.BigInteger;
import java.security.MessageDigest;

public class EncryptUtility
{
    public enum HashType
    {
        MD5("MD5"), SHA1("SHA-1");

        private final String val;

        private HashType(String val)
        {
            this.val = val;
        }

        public String val()
        {
            return this.val;
        }
    }

    public static String base64Encode(String str)
    {
        return new String(Base64.encodeBase64(str.getBytes()));
    }

    public static String base64Decode(String str)
    {
        return new String(Base64.decodeBase64(str.getBytes()));
    }

    public static String encrypt(String str, HashType hashType)
    {
        try
        {
            MessageDigest crypt = MessageDigest.getInstance(hashType.val());
            crypt.reset();
            crypt.update(str.getBytes("UTF-8"));

            return new BigInteger(1, crypt.digest()).toString(16);
        }
        catch(Exception e) { }
        return "";
    }
}
